package bma.common.netty.telnetserver.common;

import java.util.Random;

import bma.common.langutil.ai.vm.AIVM;
import bma.common.langutil.concurrent.ProcessTimerTaskAbstract;
import bma.common.langutil.concurrent.TimerManager;
import bma.common.langutil.core.MainThread;
import bma.common.langutil.core.StringUtil;
import bma.common.langutil.core.ValueUtil;
import bma.common.langutil.jmshell.JMShellUtil;
import bma.common.langutil.jmshell.Session;
import bma.common.langutil.jmshell.cmdopt.CommandOptionParser;
import bma.common.langutil.jmshell.command.AbstractSimpleCommandProcessor;

public class JMShellProcessor4Shutdown extends AbstractSimpleCommandProcessor {

	private String confirmWord;
	private long delay = 2 * 1000;

	public long getDelay() {
		return delay;
	}

	public void setDelay(long delay) {
		this.delay = delay;
	}

	public String getConfirmWord() {
		return confirmWord;
	}

	public void setConfirmWord(String confirmWord) {
		this.confirmWord = confirmWord;
	}

	public JMShellProcessor4Shutdown() {
		super("shutdown");
		setArgsTips("[shutdownConfirmWord]");
	}

	@Override
	public CommandOptionParser createCommandOption(Session session) {
		CommandOptionParser r = new CommandOptionParser();
		r.addBooleanOption('c', "confirm", "create the confirm word");
		return r;
	}

	@Override
	public boolean processCommand(Session session, String cmd,
			CommandOptionParser options) {

		if (!JMShellUtil.checkAccept(session, "shutdown")) {
			return true;
		}

		Boolean cword = options.getOptionValue("confirm", Boolean.FALSE);
		if (cword) {
			createConfirmWorld(session);
			return true;
		}
		String[] args = options.getRemainingArgs();
		if (args != null && args.length > 0) {
			shutdown(session, args[0]);
			return true;
		}
		session.writeln("ERROR: please input confirm word");
		return true;
	}

	public void createConfirmWorld(Session s) {
		int v = (new Random().nextInt(999999 - 100000)) + 100000;
		String cword = "" + v;
		s.setAttribute(getClass().getName() + "_confirmWord", cword);
		s.writeln("confirm word => " + cword);
	}

	public void shutdown(Session s, String confirmWord) {
		boolean cando = false;
		String cword = this.confirmWord;
		if (ValueUtil.notEmpty(cword)) {
			if (StringUtil.equals(confirmWord, cword)) {
				cando = true;
			}
		} else {
			cword = ValueUtil
					.toString(
							s.getAttribute(getClass().getName()
									+ "_confirmWord"), null);
			if (ValueUtil.empty(cword)) {
				s.writeln("ERROR: please create confirm word");
				return;
			}
			if (StringUtil.equals(confirmWord, cword)) {
				cando = true;
				return;
			}
		}

		if (!cando) {
			s.writeln("ERROR: input confirm word invalid");
			return;
		}
		final MainThread main = MainThread.get();
		if (main == null) {
			s.writeln("ERROR: invalid AIMain");
			return;
		}
		s.writeln("shutdown done");

		AIVM vm = AIVM.main();
		if (vm != null) {
			TimerManager tm = vm.getTimer();
			if (tm != null) {
				tm.postTimerTask(new ProcessTimerTaskAbstract(System
						.currentTimeMillis() + delay, true) {

					@Override
					public void run() {
						main.postShutdown();
					}
				});
				return;
			}
		}
		main.postShutdown();
	}
}
